﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Data;
using System.Windows.Forms;



namespace SharePointListSync
{
    public class DataManager
    {

        DataManagerVO dmanagerVO = new DataManagerVO();
        VOlistDetails mVOlistDetails = null; 
        const string errorColumnInUpdateLog = "Error";
        //public ErrorCode ResulterrorCode; 
        
        //private bool iserror; 
        #region CRUD

        internal XmlDocument CreateBatchDefination()
        {
            XmlDocument batchXml = new XmlDocument();
            XmlElement rootNode = batchXml.CreateElement("Batch");
            XmlAttribute attribute = batchXml.CreateAttribute("OnError");
            attribute.InnerText = "Continue";
            rootNode.Attributes.Append(attribute);
            batchXml.AppendChild(rootNode);
            return batchXml;
        }

        internal void Batchcmd(DataRowView drv, ref XmlDocument batch, ref VOlistDetails mVOlistDetails, DataRowState dataRowState)
        {

            XmlElement methodNode = batch.CreateElement("Method");
            XmlAttribute attribute = batch.CreateAttribute("ID");
            attribute.InnerText = (batch.DocumentElement.ChildNodes.Count + 1).ToString();
            methodNode.Attributes.Append(attribute);
            attribute = batch.CreateAttribute("Cmd");
            switch (dataRowState)
            {
                case DataRowState.Added:
                    attribute.InnerText = "New";
                     methodNode.Attributes.Append(attribute);
                     CreateFieldNode(ref methodNode, ref drv, ref mVOlistDetails);
                    break;
                
                case DataRowState.Modified:
                    hlp_IsRowDeleted(ref drv, ref attribute, ref methodNode, ref mVOlistDetails);
                    
                    break;

                case DataRowState.Deleted:
                    attribute.InnerText = "Delete";
                    methodNode.Attributes.Append(attribute);
                     XmlElement fieldNode   = methodNode.OwnerDocument.CreateElement("Field");        
                    XmlAttribute attribute1 = methodNode.OwnerDocument.CreateAttribute("Name");
                    attribute1.InnerText = "ID"; 
                    fieldNode.Attributes.Append(attribute1);
                    XmlText valueNode = methodNode.OwnerDocument.CreateTextNode(drv["ListItemID"].ToString());
                    fieldNode.AppendChild(valueNode);
                     methodNode.AppendChild(fieldNode);
                    break;
                
                
               
                default:
                    break;
            }
           
            batch.DocumentElement.AppendChild(methodNode);

        }

        private void hlp_IsRowDeleted(ref DataRowView drv, ref XmlAttribute attribute, ref XmlElement methodNode, ref VOlistDetails mVOlistDetails)
        {

            DataRowView Currentdrv = drv;
            Currentdrv.DataView.RowStateFilter = DataViewRowState.ModifiedCurrent;
            SharePointListDetails.SPResultRow Currentrow = (SharePointListDetails.SPResultRow)Currentdrv.Row;

            SharePointListDetails.SPResultRow row = (SharePointListDetails.SPResultRow)drv.Row;
            bool isDeleted = true;
            foreach (DataColumn _column in Currentrow.Table.Columns)
            {
                if(  _column.ColumnName != "ID")
                {
                    if (!string.IsNullOrEmpty(row[_column].ToString())) 
                    isDeleted = false;
                    break; 
                }
            }

            if (isDeleted)
            {
                attribute.InnerText = "Delete";
                methodNode.Attributes.Append(attribute);
                XmlElement fieldNode = methodNode.OwnerDocument.CreateElement("Field");
                XmlAttribute attribute1 = methodNode.OwnerDocument.CreateAttribute("Name");
                attribute1.InnerText = "ID";
                fieldNode.Attributes.Append(attribute1);
                string  ListItemID  = string.Empty;
                if (string.IsNullOrEmpty(drv["ListItemID"].ToString()))
                {
                    DataRowView tempdrv = drv;
                    tempdrv.DataView.RowStateFilter = DataViewRowState.ModifiedOriginal;
                    ListItemID = drv["ListItemID"].ToString();
                }
                else
                {
                    ListItemID = drv["ListItemID"].ToString();
                }
                XmlText valueNode = methodNode.OwnerDocument.CreateTextNode(ListItemID);
                fieldNode.AppendChild(valueNode);
                methodNode.AppendChild(fieldNode);
            
            }
            else
            {
                attribute.InnerText = "Update";
                methodNode.Attributes.Append(attribute);
                CreateFieldNode(ref methodNode, ref drv, ref mVOlistDetails);
            }


        }

       

        

        #region tobe deleted
        //internal void BatchAdd(System.Data.DataRowView drv, ref XmlDocument batchXml, ref VOlistDetails mVOlistDetails)
        //{
        //    XmlElement methodNode = batchXml.CreateElement("Method");
        //    XmlAttribute attribute = batchXml.CreateAttribute("ID");
        //    attribute.InnerText = (batchXml.DocumentElement.ChildNodes.Count + 1).ToString();
        //    methodNode.Attributes.Append(attribute);
        //    attribute = batchXml.CreateAttribute("Cmd");
        //    attribute.InnerText = "New";
        //    methodNode.Attributes.Append(attribute);
        //    CreateFieldNode(ref methodNode, ref drv, ref mVOlistDetails);
        //    batchXml.DocumentElement.AppendChild(methodNode);
        //}
        //internal void BatchUpdate(DataRowView drv, ref XmlDocument batch, ref VOlistDetails mVOlistDetails)
        //{
        //    XmlElement methodNode = batch.CreateElement("Method");
        //    XmlAttribute attribute = batch.CreateAttribute("ID");
        //    attribute.InnerText = (batch.DocumentElement.ChildNodes.Count + 1).ToString();
        //    methodNode.Attributes.Append(attribute);
        //    attribute = batch.CreateAttribute("Cmd");
        //    attribute.InnerText = "Update";
        //    CreateFieldNode(ref methodNode, ref drv, ref mVOlistDetails);
        //    methodNode.Attributes.Append(attribute);
        //}

        //internal void BatchDelete(DataRowView drv, ref XmlDocument batch, ref VOlistDetails mVOlistDetails)
        //{
        //    XmlElement methodNode = batch.CreateElement("Method");
        //    XmlAttribute attribute = batch.CreateAttribute("ID");
        //    attribute.InnerText = (batch.DocumentElement.ChildNodes.Count + 1).ToString();
        //    methodNode.Attributes.Append(attribute);
        //    attribute = batch.CreateAttribute("Cmd");
        //    attribute.InnerText = "Delete";
        //    methodNode.Attributes.Append(attribute);
        //    CreateFieldNode(ref methodNode, ref drv, ref mVOlistDetails);
        //    batch.DocumentElement.AppendChild(methodNode);
        //} 
        #endregion

        private void CreateFieldNode(ref XmlElement methodNode, ref DataRowView projectRow, ref VOlistDetails mVOlistDetails)
        {
            Dictionary<string, string> allcolumn = null;
            allcolumn = mVOlistDetails.ColumnToShow;
            StringBuilder sb = new StringBuilder();

            foreach (KeyValuePair<string, string> column in allcolumn)
            {
                XmlElement fieldNode = methodNode.OwnerDocument.CreateElement("Field");
                XmlAttribute attribute = methodNode.OwnerDocument.CreateAttribute("Name");
                attribute.InnerText = column.Value;
                fieldNode.Attributes.Append(attribute);
                XmlText valueNode = methodNode.OwnerDocument.CreateTextNode(projectRow[column.Key].ToString());
                fieldNode.AppendChild(valueNode);
                methodNode.AppendChild(fieldNode);
            };
        }
        internal void CommitBatch(XmlDocument batchXml, ref VOlistDetails _mVOlistDetails)
        {
            mVOlistDetails = _mVOlistDetails;
            WSlists.Lists listService = new WSlists.Lists();
            listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
            listService.Url = mVOlistDetails.WebURL + "/_vti_bin/lists.asmx";
            listService.UpdateListItemsCompleted += new WSlists.UpdateListItemsCompletedEventHandler(listService_UpdateListItemsCompleted);
            listService.UpdateListItemsAsync(mVOlistDetails.ListID, batchXml.DocumentElement);
        }

        void listService_UpdateListItemsCompleted(object sender, WSlists.UpdateListItemsCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                ErrorCode exceptionCode = ErrorCode.Exception;
                createColumnsInUpdateLogTable(exceptionCode);
                hlp_CreateLogForException(e);
               
            }

            else
            {
                ErrorCode errorCode = ErrorCode.Error;
                createColumnsInUpdateLogTable(errorCode);

                XmlTextReader xr = new XmlTextReader(e.Result.OuterXml, XmlNodeType.Element, null);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xr);

                XmlNamespaceManager xmlnamespace = new XmlNamespaceManager(xmlDoc.NameTable);
                xmlnamespace.AddNamespace("z", "#RowsetSchema");

                XmlNodeList resultNode = xmlDoc.GetElementsByTagName("Result");
                hlp_PopulateUpdateLogForEachRow(resultNode);

                

                switch (Globals.ThisWorkbook.ResulterrorCode)
                {
                    case ErrorCode.Error:
                        MessageBox.Show("Update Unsuccessful for few rows ");
                        break;
                    case ErrorCode.Exception:
                        MessageBox.Show("Error Ocured during Update None of row got update , Please Try later or contact your system admin ");
                        break;
                    case ErrorCode.Success:
                       MessageBox.Show("Update Successful");
                        break;
                    default:
                        break;
                }

            }

            Globals.Sheet2.list1.DataSource = Globals.ThisWorkbook.DSlists.UpdateLog;
            Globals.Sheet2.list1.AutoSelectRows = true;
            Globals.Sheet2.list1.AutoSetDataBoundColumnHeaders = true;
        }


        #region Curd Helper Methods
        private void createColumnsInUpdateLogTable(ErrorCode UpdateLog)
        {
            //Globals.ThisWorkbook.DSlists.UpdateLog.Rows.Clear();
            //Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Clear();
            //Globals.ThisWorkbook.DSlists.UpdateLog.AcceptChanges(); 

            switch (UpdateLog)
            {
                case ErrorCode.Exception:
                    foreach (KeyValuePair<string, string> columns in mVOlistDetails.ColumnToShow)
                    {
                        Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Add(columns.Key);
                    }
                    Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Add(errorColumnInUpdateLog);
                    Globals.ThisWorkbook.DSlists.UpdateLog.AcceptChanges();
                    break;


                case ErrorCode.Error:
                    
                    foreach (KeyValuePair<string, string> columns in mVOlistDetails.ColumnToShow)
                    {
                        DataColumn newcolumn = new DataColumn(columns.Key);
                        
                        if(!Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Contains(newcolumn.ColumnName))
                        Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Add(newcolumn);
                    }
                    DataColumn newcolumnUpdateStatus = new DataColumn("UpdateStatus");                    
                    if (!Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Contains(newcolumnUpdateStatus.ColumnName))
                    Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Add(newcolumnUpdateStatus);

                    DataColumn newcolumnErrorCode = new DataColumn("ErrorCode");
                    if (!Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Contains(newcolumnErrorCode.ColumnName))
                    Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Add(newcolumnErrorCode);


                    DataColumn newcolumnErrorDetails = new DataColumn("ErrorDetails");
                    if (!Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Contains(newcolumnErrorDetails.ColumnName))
                    Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Add(newcolumnErrorDetails);

                    DataColumn newcolumnRowID = new DataColumn("RowID");
                    if (!Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Contains(newcolumnRowID.ColumnName))
                    Globals.ThisWorkbook.DSlists.UpdateLog.Columns.Add(newcolumnRowID);

                    Globals.ThisWorkbook.DSlists.UpdateLog.AcceptChanges();
                    break;
                default: 
                    break; 
            }
        }
        private void hlp_PopulateUpdateLogForEachRow(XmlNodeList resultNode)
        {

            
            foreach (XmlNode _result in resultNode)
            {
                SharePointListDetails.UpdateLogRow row = Globals.ThisWorkbook.DSlists.UpdateLog.NewUpdateLogRow();
                row["RowID"] = _result.Attributes["ID"].InnerText;
                foreach (XmlNode detailsOfResult in _result.ChildNodes)
                {
                    switch (detailsOfResult.Name)
                    {
                        case "ErrorCode":
                            string errorCode = detailsOfResult.InnerText;
                            if (errorCode == "0x00000000")
                            {
                                row["UpdateStatus"] = "Successful";
                                Globals.ThisWorkbook.ResulterrorCode = ErrorCode.Success;
                            }
                            else
                            {
                                row["UpdateStatus"] = "Unsuccessful";
                                Globals.ThisWorkbook.ResulterrorCode = ErrorCode.Error;
                            }

                            row["ErrorCode"] = errorCode;
                            break;

                        case "ErrorText":
                            row["ErrorDetails"] = detailsOfResult.Value;
                            break;

                        case "z:row":
                            Hlp_PopulateResult(detailsOfResult,row);                             
                            break;

                        default:
                            break;
                    }
                }


                Globals.ThisWorkbook.DSlists.UpdateLog.Rows.Add(row);
            }
        }



        private void Hlp_PopulateResult(XmlNode detailsOfResult, SharePointListDetails.UpdateLogRow row)
        {
            string ColumnName = string.Empty;
            string columnInternalName = string.Empty;


            foreach (DataColumn _column in Globals.ThisWorkbook.DSlists.UpdateLog.Columns)
            {
                bool addColumn = true;
                if (string.Equals(_column.ColumnName, "UpdateStatus") || string.Equals(_column.ColumnName, "ErrorCode") || string.Equals(_column.ColumnName, "RowID") || string.Equals(_column.ColumnName, "ErrorDetails"))
                {
                    addColumn = false;
                }
                if (addColumn)
                    if (mVOlistDetails.ColumnToShow.TryGetValue(_column.ColumnName, out ColumnName))
                    {

                        columnInternalName = "ows_" + ColumnName;
                        row[_column] = detailsOfResult.Attributes[columnInternalName] == null ? "NA" : detailsOfResult.Attributes[columnInternalName].InnerText;
                    }

            }
            Globals.ThisWorkbook.DSlists.UpdateLog.AcceptChanges();
        }

        private void hlp_CreateLogForException(WSlists.UpdateListItemsCompletedEventArgs e)
        {

            // get the error Details
            Globals.ThisWorkbook.ResulterrorCode = ErrorCode.Exception;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Error Message :  " + e.Error.Message);
            sb.AppendLine("InnerException  :  " + e.Error.InnerException);
            sb.AppendLine("Stack   :  " + e.Error.StackTrace);
            string ErrorDetails = sb.ToString();
            sb = null;

            // Create Datarow 

            SharePointListDetails.UpdateLogRow resultrow = Globals.ThisWorkbook.DSlists.UpdateLog.NewUpdateLogRow();
            foreach (DataColumn columnName in Globals.ThisWorkbook.DSlists.UpdateLog.Columns)
            {
                if (columnName.ColumnName == errorColumnInUpdateLog)
                    resultrow[columnName] = ErrorDetails;
                else
                    resultrow[columnName] = "Exception Occured While Update Opration";

            }
            Globals.ThisWorkbook.DSlists.UpdateLog.Rows.Add(resultrow);
            Globals.ThisWorkbook.DSlists.UpdateLog.AcceptChanges();
        }


        #endregion


        #endregion 


        #region Helper method


        private XmlNode callListService()
        {
            if (!dmanagerVO.isInitalizedforEveryWebMethodCalled)
            {
                throw new Exception("Please call InitalizedforEveryWebMethod of datamanagerVO before calling callListService "); 
            }

            
            XmlNode returnnode = null;

            WSlists.Lists listService = new WSlists.Lists();
            listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
            listService.Url = dmanagerVO.WebURL + "/_vti_bin/lists.asmx";

            switch (dmanagerVO.WebMethod)
            {
                case "GetListContentTypes":
                    returnnode = listService.GetListContentTypes(dmanagerVO.ListID, null);
                    break;

                case "GetList":
                    returnnode = listService.GetList(dmanagerVO.ListID);
                    break;

                case "GetListItems":
                    returnnode = listService.GetListItems(dmanagerVO.ListID, dmanagerVO.ViewID, dmanagerVO.ndQuery, dmanagerVO.ndViewFields, dmanagerVO.RowLimit, dmanagerVO.ndQueryOptions, dmanagerVO.WebID);
                    break;

                case "GetViewCollection3":
                    break;

                case "GetViewCollection4":
                    break;

                default:
                    break;
            }

            dmanagerVO.resetBeforeUseVO();
            return returnnode;
        }        

        private XmlNode callViewService()
        {
            if (!dmanagerVO.isInitalizedforEveryWebMethodCalled)
            {
                throw new Exception("Please call InitalizedforEveryWebMethod of datamanagerVO before calling callViewService ");
            }
            
            XmlNode returnnode = null;


            WSView.Views viewService = new WSView.Views();
            viewService.Credentials = System.Net.CredentialCache.DefaultCredentials;
            viewService.Url = dmanagerVO.WebURL + "/_vti_bin/views.asmx";

            switch (dmanagerVO.WebMethod)
            {
                case "GetViewCollection":
                    returnnode = viewService.GetViewCollection(dmanagerVO.ListName);
                    break;

                case "GetView":

                    returnnode = viewService.GetView(dmanagerVO.ListName, dmanagerVO.ViewID);
                    break;

                case "GetViewCollection2":
                    break;

                case "GetViewCollection3":
                    break;

                case "GetViewCollection4":
                    break;

                default:
                    break;
            }
            dmanagerVO.resetBeforeUseVO();
            return returnnode;


        }

        private Dictionary<string, string> XMLNodeToDictionary(XmlNode node, string Key, string Value)
        {
            Dictionary<string, string> AllViews = new Dictionary<string, string>();
            foreach (XmlNode mview in node.ChildNodes)
            {
                if (mview.Attributes[Key] != null && mview.Attributes[Value] != null)
                {
                    AllViews.Add(mview.Attributes[Key].InnerText, mview.Attributes[Value].InnerText);
                }
            }
            return AllViews;
        }
        #endregion


        #region ACLISTSelector
        internal Dictionary<string, string> GetAllViews(string ListName, string WebURL)
        {
            dmanagerVO.WebURL = WebURL;
            dmanagerVO.ListName = ListName;
            dmanagerVO.InitalizedforEveryWebMethod("GetViewCollection");
            XmlNode nodeViews = callViewService();
            return XMLNodeToDictionary(nodeViews, "DisplayName", "Name");            
        }

        internal Dictionary<string, string> GetAllContentType(string ListID, string WebURL)
        {
            //Dictionary<string, string> AllContentType = new Dictionary<string, string>();            
            dmanagerVO.ListID = ListID;
            dmanagerVO.WebURL = WebURL;
            dmanagerVO.InitalizedforEveryWebMethod("GetListContentTypes"); 
            XmlNode nodelistContentType = callListService();
            return XMLNodeToDictionary(nodelistContentType, "Name", "ID");
        }

        internal Dictionary<string, string> GetListColumns(SharePointListDetails.SPConnectionsRow Selectedlist)
        {

            dmanagerVO.ListID = Selectedlist.ListID;
            dmanagerVO.WebURL = Selectedlist.WebURL;
            dmanagerVO.InitalizedforEveryWebMethod("GetList"); 
            XmlNode ndQuery = callListService();
            
            Dictionary<string, string> Allcolumns = new Dictionary<string, string>();
            XmlNodeList XnallFields = ndQuery.ChildNodes[0].ChildNodes;

            foreach (XmlNode Xnfield in XnallFields)
            {
                if (shouldAdd(Xnfield))
                {
                    if (!Allcolumns.ContainsKey(Xnfield.Attributes["DisplayName"].InnerText))
                    {
                        Allcolumns.Add(Xnfield.Attributes["DisplayName"].InnerText, Xnfield.Attributes["StaticName"].InnerText);
                    }
                    else
                    {
                        Allcolumns.Add(Xnfield.Attributes["Name"].InnerText, Xnfield.Attributes["StaticName"].InnerText);
                    }
                }
            }
            return Allcolumns;
        }

        private bool shouldAdd(XmlNode Xnfield)
        {
            bool shouldAdd = true;
           
            // to be remove 
            if (Xnfield.Attributes["ColName"] == null)
            {
                shouldAdd = false;
                return shouldAdd;
            }

            if (Xnfield.Attributes["Type"] == null)
            {
                shouldAdd = false;
                return shouldAdd;
            }

            if (Xnfield.Attributes["Type"].InnerText == "ModStat"
                 || Xnfield.Attributes["Type"].InnerText == "Guid")
            {
                shouldAdd = false;
                return shouldAdd;
            }

            if (Xnfield.Attributes["Hidden"] != null
                && Xnfield.Attributes["Hidden"].InnerText == "TRUE")
            {
                shouldAdd = false;
                return shouldAdd;
            }

            if (Xnfield.Attributes["ShowInFileDlg"] != null)
            {
                shouldAdd = false;
                return shouldAdd;
            }

            if (Xnfield.Attributes["StaticName"].InnerText == "_UIVersionString")
            {
                shouldAdd = false;
                return shouldAdd;
            }

            return shouldAdd;
        }

        #endregion

        internal Dictionary<string, string> GetAllColumnofView(string ListName, string ViewID, string WebURL)
        {
            Dictionary<string, string> AllColumn = new Dictionary<string, string>();
            WSView.Views viewService = new WSView.Views();
            viewService.Credentials = System.Net.CredentialCache.DefaultCredentials;
            viewService.Url = WebURL + "/_vti_bin/views.asmx";
            XmlNode nodeViews = viewService.GetView(ListName, ViewID);
            XPathNavigator xnav = nodeViews.CreateNavigator();

            xnav.MoveToChild(XPathNodeType.Element);
            xnav.MoveToNext();
            xnav.MoveToChild(XPathNodeType.Element);
            do
            {
                xnav.MoveToFirstAttribute();
                AllColumn.Add(getNormilixedstring(xnav.Value),xnav.Value);
                xnav.MoveToParent();

            } while (xnav.MoveToNext()); 

            return AllColumn; 
        }

        private string getNormilixedstring(string rawValue)
        {
            return rawValue.Replace("_x0020_", " "); 
        }

        internal XmlNode GetListItems(SharePointListDetails.SPConnectionsRow ListRow, string ViewID, string ViewName, string CTName, string CTID , int _rowLimit)
        {

            dmanagerVO.WebURL = ListRow.WebURL;
            dmanagerVO.WebID = ListRow.WebID;
            dmanagerVO.ListID = ListRow.ListID;
            dmanagerVO.ViewID = ViewID;
            dmanagerVO.RowLimit = _rowLimit.ToString(); 
            dmanagerVO.InitalizedforEveryWebMethod("GetListItems");
            XmlNode allResult = callListService();             
            return allResult; 
        }

        #region All Column
        internal XmlNode GetListItemsAllColumn(SharePointListDetails.SPConnectionsRow ListRow, Dictionary<string, string> selectedColumns, string ViewID, string ViewName, string CTName, string CTID, int _rowLimit)
        {
            dmanagerVO.WebURL = ListRow.WebURL;
            dmanagerVO.WebID = ListRow.WebID;

            dmanagerVO.ListID = ListRow.ListID;
            dmanagerVO.ListName = ListRow.ListName;
            dmanagerVO.RowLimit = _rowLimit.ToString(); 

            dmanagerVO.strViewFields = getViewField(selectedColumns);

            dmanagerVO.InitalizedforEveryWebMethod("GetListItems");
            XmlNode allResult =  callListService(); 
            return allResult; 
        }

        private string getViewField(Dictionary<string, string> selectedColumns)
        {
            StringBuilder sb = new StringBuilder();
            //  "<FieldRef Name='Title'/><FieldRef Name='ProjectNumber'/><FieldRef Name='StartDate'/><FieldRef Name='Budget'/>"
            foreach (KeyValuePair<string, string> column in selectedColumns)
            {
                sb.AppendFormat("<FieldRef Name='{0}'/>", column.Value);
            }
            return sb.ToString();
        } 
        #endregion


        
    

    }
}
